from lightgbm.sklearn import LGBMRegressor
from sklearn.model_selection import cross_val_score
from sklearn.metrics import mean_absolute_error,  make_scorer
import pandas as pd
import numpy as np


def main(args):
    cat = {
        'model': 'category',
        'brand': 'category',
        'bodyType': 'category',
        'fuelType': 'category',
        'gearbox': 'category',
        'notRepairedDamage': 'category',
        'regionCode': 'category',
    }

    df = pd.read_csv('./user_data/df_s.csv', sep=' ', dtype=cat)
    #df['regionCode_count'] = pd.qcut(df.groupby(['regionCode'])['SaleID'].transform('count'), q=10,labels=range(10))
    #df['city'] = pd.Categorical(df['regionCode'].apply(lambda x: str(x)[:2]))

    train_X = df[df.train == 1].drop(['price', 'SaleID', 'regionCode'], axis=1)
    train_y = df[df.train == 1]['price']
    train_y_ln = np.log1p(train_y)

    model = LGBMRegressor(**args)
    '''
    test_X = df[df.train == 0].drop(['price', 'SaleID', 'regionCode'], axis=1)
    model.fit(X=train_X,
              y=train_y_ln)
    p = model.predict(test_X)
    submit = pd.DataFrame()
    submit['SaleID'] = df[df.train == 0].SaleID
    submit['price'] = np.expm1(p)
    submit.to_csv('./prediction_result/submit.csv', index=0)
    print('end for predict')
    return
    '''
    n = 1

    def maee(y_true, y_pred):
        nonlocal n
        loss = mean_absolute_error(np.expm1(y_true), np.expm1(y_pred))
        print(f'mae in fold {n}:{loss}')
        n += 1
        return loss

    mae = cross_val_score(model,
                          X=train_X,
                          y=train_y_ln,
                          verbose=0,
                          cv=5,
                          scoring=make_scorer(maee))
    print(f'{mae}')
    print(f'with mae.mean = {np.mean(mae)}')


if __name__ == '__main__':
    param = {'boosting_type': 'gbdt',
             'num_leaves': 55,  # 过小容易过拟合，过大则计算时间长
             'max_depth': 35,
             "lambda_l2": 2,  # 防止过拟合
             'min_data_in_leaf': 20,  # 防止过拟合，好像都不用怎么调
             'objective': 'regression_l1',
             'learning_rate': 0.02,
             "min_child_samples": 20,
             'n_estimators': 15000,
             "feature_fraction": 0.7,
             "bagging_freq": 1,
             "bagging_fraction": 0.9,
             "bagging_seed": 11,
             "metric": 'mae',
             }
    main(param)
